/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.escom.healthlc.security;

import com.escom.healthlc.utils.Constantes;
import com.sun.faces.util.Util;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;
import java.util.logging.Level;
import javax.faces.application.Resource;
import javax.faces.application.ResourceHandler;
import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class MediaResource extends Resource {

    private static final Logger logger = LoggerFactory.getLogger(MediaResource.class);

    private final String mediaResource;
    private final String libraryName;
    private String pathApplication;

    public MediaResource(final String libraryName, final String mediaResource, String pathApplication) {
//        logger.debug("libraryName {} mediaResourcee {} pathApplication {}", new String[]{libraryName, mediaResource, pathApplication});
//        logger.debug("mediaResourcee {}", mediaResource);
//        logger.debug("pathApplication {}", pathApplication);
        setLibraryName(libraryName);
        setResourceName(mediaResource);
        if (mediaResource.contains(".js")) {
            setContentType("text/javascript");
        } else if (mediaResource.contains(".css")) {
            setContentType("text/css");
        }
        this.libraryName = libraryName;
        this.mediaResource = mediaResource;
        this.pathApplication = pathApplication;
    }

    @Override
    @SuppressWarnings("empty-statement")
    public InputStream getInputStream() {
        String pathResource = pathApplication + "\\resources\\" + libraryName + "\\" + mediaResource;
        pathResource = pathResource.replace("/", "\\");
        logger.debug("pathResource {}", pathResource);
        File file = new File(pathResource);
        FileInputStream fileInputStream = null;
        try {
            fileInputStream = new FileInputStream(file);
        } catch (FileNotFoundException ex) {
            logger.error("Logger Error: {}", ex.getCause().getMessage());
            logger.error("Logger Error: {}", ex.fillInStackTrace().toString());
        }
        InputStream inputStream = fileInputStream;
        return inputStream;
    }

    @Override
    public Map<String, String> getResponseHeaders() {
//        logger.debug("getResponseHeaders");
        return new HashMap<>();
    }

    @Override
    public String getRequestPath() {
        final FacesContext context = FacesContext.getCurrentInstance();
        String requestPath = context.getApplication().getViewHandler().getResourceURL(context,
                ResourceHandler.RESOURCE_IDENTIFIER + "/" + mediaResource + Util.getFacesMapping(context)
                + "?ln=" + libraryName);
//        logger.debug("getRequestPath {}", requestPath);
        return requestPath;
    }

    @Override
    public URL getURL() {
        ExternalContext context = FacesContext.getCurrentInstance().getExternalContext();
        StringBuilder buf = new StringBuilder(context.getRequestScheme());
        buf.append(context.getRequestServerName());
        if (context.getRequestServerPort() != 80 && context.getRequestServerPort() != 443) {
            buf.append(":").append(context.getRequestServerPort());
        }
        buf.append(getRequestPath());
        URL url = null;
        try {
            url = new URL(buf.toString());
//            logger.debug("Created new URL " + buf.toString() + " for ProgramResource " + this.toString());
        } catch (java.net.MalformedURLException e) {
//            logger.error("Unable to create URL for ProgramResource " + this.toString(), e);
        }
        return url;
    }

    @Override
    public boolean userAgentNeedsUpdate(FacesContext fc) {
        return true;
    }

}
